#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>

namespace U32
{
	using namespace std;
	class Statistics
	{
	private:
		static const int size = 1000001;
	public:
		array<int,size> *salaries;

		Statistics(void)
		{
			this->salaries = new array<int,size>();
		}

		~Statistics(void)
		{
			delete [] salaries;
		}

		void clear()
		{
			for( auto i : (*salaries) )
				(*salaries)[i] = 0;
		}

		void ReadFromFile( string filename )
		{
			ifstream fin;
			string strRow;
			int tempInt;
			try
			{
				fin.open( filename );

				while( getline( fin, strRow ) )
				{
					istringstream iss(strRow);
					iss >> tempInt;

					(*salaries)[tempInt]++;
				}
			}
			catch( exception e )
			{
				cout << "Exception" << filename << endl;
				cout << e.what() << endl;
			}

			fin.close();
		}

		int CalculateMean()
		{
			int numberOfItems = 0;
			long long int totalValue = 0;
			for( int i = 20000; i < 1000001; i++ )
			{
				totalValue += i * (*salaries)[i];
				numberOfItems += (*salaries)[i];
			}

			return float(totalValue) / numberOfItems;
		}

		int CalculateMedian()
		{
			int numberOfItems = 0;

			sort( salaries->begin(), salaries->end() );

			for( int i = 20000; i < 1000001; i++ )
			{
				numberOfItems += (*salaries)[i];
			}

			int middle = 0;

			for( int i = 20000; i < 1000001; i++ )
			{
				middle += (*salaries)[i];
				if( middle >= (numberOfItems / 2 ) )
					return i;
			}

			return 0;
			
		}
	};


}